<?php
class WebsitesAction extends CommonAction {  
	private $statusArray = array(1=>'已审核',2=>'待审核',3=>'黑名单');
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Websites');
		$this->modelApplyObject = D('Webapply');
		$this->modelViewObject = D('WebsitesView');
		$this->modelDataObject = D('Webdata');
		
		$this->assign('statusArray', $this->statusArray);
		$this->assign('cateOption', R('WebsitesCate/getCateOption'));
    }
	
    public function index(){
		$thisPage = $this->pageInfo['p'] = I('p', 1);
		
		if($this->pageInfo['name'] = I('name', '')){
			$map['name'] = array('LIKE', '%'.$this->pageInfo['name'].'%');
		}
		
		if($this->pageInfo['status'] = I('status', '')){
			$map['status'] = $this->pageInfo['status'];
		}
		
		if($this->pageInfo['cate_id'] = I('cate_id', '')){
			$map['cate_id'] = $this->pageInfo['cate_id'];
		}
		
		if($this->pageInfo['attr'] = I('attr', '')){//属性选择
			$map[$this->pageInfo['attr']] = 1;
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('pageInfo', $this->pageInfo);
		$this->assign('pageInfo', $this->pageInfo);
		$this->assign('list', $list);
		
		$this->assign('categoryTextArray', D('WebsitesCate')->getAllCateTextArray());
		
		$this->display();
    }
	
	public function add(){
		if(IS_POST){
			$applyId    = I('apply_id', 0, 'intval');
			$cateId     = I('cate_id', 0, 'intval');
			$provinceId = I('province_id', 0, 'intval');
			$cityId     = I('city_id', 0, 'intval');
			$domain 	    = I('domain', '', 'strval');
			if (empty($domain)) {
				$this->error('请输入网站域名!');
			} else {
				if (!isDomain($domain)) {
					$this->error('请输入正确的网站域名!');
				}
			}
			
			//查询域名是否存在
			if($this->modelViewObject->getInfo(array('domain'=>$domain))){
				$this->error('该网站已经存在,不能重复添加!');		
			}
			if($cateId == 0){
				$this->error('请选择分类!');	
			}
			if($provinceId == 0 || $cityId == 0){
				$this->error('请选择网站所属地区!');	
			}
			
			
			//获取附表内容
			$contentData = array();
			$contentData['ip'] = I('ip', '');
			$contentData['brank'] = I('brank', 0);
			$contentData['grank'] = I('grank', 0);
			$contentData['srank'] = I('srank', 0);
			$contentData['arank'] = I('arank', 0);
			$contentData['update_time'] = $_POST['update_time'] = $_POST['add_time'] = $_POST['in_time'] = NOW_TIME;
			
			if($this->modelObject->create()){
				if($insertId = $this->modelObject->add()){
					$contentData['web_id'] = $insertId;
					if($this->modelDataObject->add($contentData)){	
					
						if($applyId){
							$this->modelApplyObject->deleteInfo($applyId);	
						}
								
						$this->success('插入数据成功', U('index'));
					}else{
						$this->modelObject->deleteInfo($insertId);
						$this->error('数据插入附表失败');
					}
				}else{
					$this->error('插入失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$applyId = I('apply_id', 0, 'intval');
			if($applyId){
				$info = $this->modelApplyObject->getInfo($applyId);
				$this->assign('info', $info);
				$this->assign('city', R('Region/getCity', array('provinceId'=>$info['province_id'])));
			}
			
			$this->assign('province', R('Region/getProvince'));
			$this->display();	
		}
	}
	
	public function edit(){
		if(IS_POST){
			$cateId  = I('cate_id', 0, 'intval');
			$webId  = I('id', 0, 'intval');
			$provinceId = I('province_id', 0, 'intval');
			$cityId = I('city_id', 0, 'intval');
			$domain  = I('domain', '', 'strval');
			if (empty($domain)) {
				$this->error('请输入网站域名!');
			} else {
				if (!isDomain($domain)) {
					$this->error('请输入正确的网站域名!');
				}
			}
			if($cateId == 0){
				$this->error('请选择分类!');	
			}
			if($provinceId == 0 || $cityId == 0){
				$this->error('请选择网站所属地区!');	
			}
			
			//获取附表内容
			$contentData = array();
			$contentData['ip'] = I('ip', '');
			$contentData['brank'] = I('brank', 0);
			$contentData['grank'] = I('grank', 0);
			$contentData['srank'] = I('srank', 0);
			$contentData['arank'] = I('arank', 0);
			$contentData['update_time'] = $_POST['update_time'] = NOW_TIME;
			
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					if($this->modelDataObject->where('web_id = '.$webId)->save($contentData)){				
						$this->success('编辑成功', U('index', session('pageInfo')));	
					}else{
						$this->error('附表数据编辑失败');
					}
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$id = I('id', 0, 'intval');
			if($id){
				$info = $this->modelViewObject->getInfo($id);
				$this->assign('info', $info);
				$this->assign('province', R('Region/getProvince'));
				$this->assign('city', R('Region/getCity', array('provinceId'=>$info['province_id'])));
				$this->display();
			}else{
				$this->error('参数错误!');
			}
		}
	}
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			$info = $this->modelViewObject->getInfo($id);
			if($this->modelObject->deleteInfo($id)){
				//删除附表信息
				$this->modelDataObject->deleteInfo(array('web_id'=>$id));
				
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function delAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			$infoList = $this->modelViewObject->getAll(array('id'=>array('IN', $idStr)));
			if($this->modelObject->deleteInfo($idStr)){
				//删除附表信息
				$this->modelDataObject->deleteInfo(array('web_id'=>array('IN', $idStr)));
				
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function setStatus(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		$status = I('status', 3, 'intval');
		
		if(!empty($idStr)){
			if( $this->modelObject->where(array('id'=>array('IN', $idStr)))->setField('status', $status )){
				$this->success('修改成功', U('index', session('pageInfo')));	
			}else{
				$this->error('修改失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function getMeta(){
		$domain = I('domain', '', 'trim');
		
		if (empty($domain)) {
			$this->ajaxReturn(0, '请输入网站域名!', 0);
		} else {
			if (!isDomain($domain)) {
				$this->ajaxReturn(0, '请输入正确的网站域名!', 0);
			}
		}
		$data = array();
		$data = getUrlContent('http://'.$domain);
		
		$this->ajaxReturn($data, '返回数据', 1);
	}
	
	public function getWdata(){
		$domain = I('domain', '', 'trim');
		if (empty($domain)) {
			$this->ajaxReturn(0, '请输入网站域名!', 0);
		} else {
			if (!isDomain($domain)) {
				$this->ajaxReturn(0, '请输入正确的网站域名!', 0);
			}
		}
		
		$data = array();
		$data['ip'] = get_server_ip($domain);
		$data['brank'] = get_baidu_rank($domain);
		$data['grank'] = get_google_rank($domain);
		$data['srank'] = get_sogou_rank($domain);
		$data['arank'] = get_alexa_rank($domain);
		$this->ajaxReturn($data, '返回数据', 1);
	}
}//end class